package cuongnh.project.japanesediary.vocabulary.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cuongnh.project.japanesediary.vocabulary.model.Context;

@Repository
@Transactional
public class ContextDao extends HibernateDaoSupport {

	public void save(Context a1) {
		a1.setCreatedDate(new Date());
		a1.setModifiedDate(new Date());
		getHibernateTemplate().save(a1);
	}
	
	public void saveOrUpdate(Context a1) {
		a1.setModifiedDate(new Date());
		if(a1.getCreatedDate() == null){
			a1.setCreatedDate(new Date());
		}
		getHibernateTemplate().saveOrUpdate(a1);
	}

	/**
	 * 'load' returns a proxy with only the id populated. To access members in
	 * the proxy requires a Hibernate session. Haven't figured out how to do
	 * transactions between bundles yet...
	 */
	public Context load(long id) {
		return (Context) getHibernateTemplate().load(Context.class, id);
	}

	public Context get(long id) {
		return (Context) getHibernateTemplate().get(Context.class, id);
	}

	public void update(Context a1) {
		a1.setModifiedDate(new Date());
		getHibernateTemplate().update(a1);
	}

	/**
	 * Delete current category with all association function
	 * 
	 * @param a1
	 *            : Current category
	 */
	public void delete(Context a1) {
		getHibernateTemplate().delete(a1);
	}

	public Context merge(Context a1) {
		a1.setModifiedDate(new Date());
		return (Context) getHibernateTemplate().merge(a1);
	}

	public List<Context> getAll() {
		List<Context> result = new ArrayList<Context>();
		result = getHibernateTemplate().loadAll(Context.class);
		if(result == null || result.isEmpty()){
			return new ArrayList<Context>();
		}
		
		return result;
	}

	public Set<Context> getAllParentWithSubContexts() {
		Set<Context> result = new HashSet<Context>();
		List<Context> all = getAll();
		if(all != null && !all.isEmpty()){
			for (Context context : all) {
				if (context.getParentContext() == null) {
					initAllSubContexts(context);
					result.add(context);
				}
			}
		}

		return result;
	}

	private void initAllSubContexts(Context context) {
		getHibernateTemplate().initialize(context.getSubContext());
		for (Context cont : context.getSubContext()) {
			initAllSubContexts(cont);
		}
	}
	
	public Context getContextWithSubContexts(long contextId) {
		Context context = getHibernateTemplate().get(Context.class,
				contextId);
		getHibernateTemplate().initialize(context.getSubContext());
		return context;
	}

	public Context getContextWithTexts(long contextId) {
		Context context = getHibernateTemplate().get(Context.class,
				contextId);
		getHibernateTemplate().initialize(context.getTexts());
		return context;
	}
	
	

	public Context getContextWithAllLazies(long contextId) {
		Context context = getHibernateTemplate().get(Context.class,
				contextId);
		getHibernateTemplate().initialize(context.getSubContext());
		getHibernateTemplate().initialize(context.getTexts());
		return context;
	}

}